package com.map
{
	public class MapGrid
	{
		public function MapGrid()
		{
		}
		
		public function get isoOffsetZ():Number {
			return (Math.sqrt(5) >> 2) * MapDatas.getInstance().map_width;
		}
		
		public var isoTiles:Array = [];
		public function genBarrier():void {
			var maxIndex:int = ((MapDatas.getInstance().map_width >> 2) + MapDatas.getInstance().map_height) / 50;
			for(var i:int = 0; i <= maxIndex; i++) {
				if(isoTiles[i] == null)
					isoTiles[i] = [];
				for(var j:int = 0; j <= maxIndex; j++) {
					if(isoTiles[i][j] == null) {
						isoTiles[i][j] = new GridVO(j, i);
					}
					if(i >= 0 && j >= MapDatas.getInstance().map_width / 200 - i && j <= maxIndex - i
					&& j <= i + MapDatas.getInstance().map_width / 200 && j >= i - MapDatas.getInstance().map_width / 200) {
						var r:Number = Math.random();
						if(r < 0.7 && !isoTiles[i][j].isValid) {
							isoTiles[i][j].isValid = true;
						}
					}
				}
			}
		}
		
		public function getLinks(grid:GridVO):Array {
			var links:Array = [];
			var startX:int = Math.max(0, grid.xIndex-1);
			var endX:int = Math.min(isoTiles[0].length, grid.xIndex+1);
			var startZ:int = Math.max(0, grid.zIndex-1);
			var endZ:int = Math.min(isoTiles.length, grid.zIndex+1);
			for(var i:int = startZ; i <= endZ; i++) {
				for(var j:int = startX; j <= endX; j++) {
					if(grid == isoTiles[i][j] || !isoTiles[i][j].isValid 
						|| !isoTiles[grid.zIndex][j].isValid || !isoTiles[i][grid.xIndex].isValid) {
						continue;
					}
					links.push(isoTiles[i][j] as GridVO);
				}
			}
			return links;
		}
		
		public function get oriX():Number {
			return -MapDatas.getInstance().map_width >> 1;
		}
		public function get oriY():Number {
			return MapDatas.getInstance().map_width >> 2;
		}
	}
}